#include<linux/module.h>
#include<linux/sched.h>
#include<linux/pid.h>
#include<linux/wait.h>
#include<linux/list.h>
#include<linux/kthread.h>
MODULE_LICENSE("GPL");

static wait_queue_head_t head;
struct task_struct *old_thread;

int my_function(void *argc)
{
	printk("in the kernel thread function!\n");
	printk("the current pid is :%d\n",current->pid);

	printk("the state of the init function is:%ld\n",old_thread->state);
	__wake_up_sync_key(&head,TASK_ALL,0,NULL);

	printk("the state of the init function after __wake_up_sync_key is :%ld\n",old_thread->state);
	printk("out the kernel thread function\n");
	return 0;	
}
static int __init __wake_up_sync_key_init(void)
{
	char namefrm[]="__wake_up_sync_key.c%s";
	long time_out;
	struct task_struct *result;
	wait_queue_t data;
	printk("into __wake_up_sync_key_init.\n");
	result=kthread_create_on_node(my_function,NULL,-1,namefrm);
	printk("the pid of the new thread is:%d\n",result->pid);
	printk("the current pid is:%d\n",current->pid);

	init_waitqueue_head(&head);
	init_waitqueue_entry(&data,current);
	add_wait_queue(&head,&data);
	old_thread=current;
	wake_up_process(result);
	time_out=schedule_timeout_uninterruptible(1000*10);

	printk("the schedule timeout is:%ld\n",time_out);
	printk("out __wake_up_sync_key_init.\n");
	return 0;
}
static void __exit __wake_up_sync_key_exit(void)
{
	printk("Goodbye __wake_up_sync_key\n");
}
module_init(__wake_up_sync_key_init);
module_exit(__wake_up_sync_key_exit);
